/**
 * @file mjnconverters.cpp
 * @brief Implementation of custom MJN types convertors for QRemoteSignal services
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 *
 * @author VestniK (Sergey N.Vidyuk) sir.vestnik@gmail.com
 * @date 10 Jan 2010
 */
#include "mjnconverters.h"

#include <QRemoteSignal>

namespace qrs {

   bool getArgValue(const QVariant &arg, QUuid &res) {
      QString uuid_str;
      if ( getArgValue(arg,uuid_str) ) {
         res = QUuid(uuid_str);
         return true;
      }
      return false;
   }

   // WindType
   QVariant createArg(mjn::WindType val) {
      switch ( val ) {
         case mjn::East:  return createArg( QString("East") );
         case mjn::South: return createArg( QString("South") );
         case mjn::West:  return createArg( QString("West") );
         case mjn::North: return createArg( QString("North") );
      }
   }

   bool getArgValue(const QVariant &arg, mjn::WindType &res) {
      QString strVal;
      if ( ! getArgValue(arg,strVal) ) {
         return false;
      }
      if ( strVal == "East" ) {
         res = mjn::East;
         return true;
      } else if ( strVal == "South" ) {
         res = mjn::South;
         return true;
      } else if ( strVal == "West" ) {
         res = mjn::West;
         return true;
      } else if ( strVal == "North" ) {
         res = mjn::North;
         return true;
      }
      return false;
   }

    QVariant createArg(const mjn::SessionInfo &val)
    {
        QVariantMap resMap;
        resMap["name"] = val.name;
        resMap["disconnected"] = val.disconnected;
        return QVariant(resMap);
    }

    bool getArgValue(const QVariant &arg, mjn::SessionInfo &res)
    {
        if (arg.type()!=QVariant::Map) return false;
        QVariantMap argMap = arg.toMap();
        if (!argMap.contains("name") || !argMap.contains("disconnected")) return false;
        res.name = argMap["name"].toString();
        res.disconnected = argMap["disconnected"].toBool();
        return true;
    }
}
